#include "devList.h"
#include "sysmanage.h"

devListDef  HostDevList[HOST_DEV_MAX_NUM] = {0};


rt_int8_t empty_pos_devlist(void)
{
	rt_int8_t i;

	for(i=0;i<HOST_DEV_MAX_NUM;i++)
	{
		if(HostDevList[i].id == 0)
		{
			return i;
		}
	}

	return  -1;
}


rt_int8_t find_dev_devlist(devListDef *dev)
{
	rt_int8_t i;

	for(i=0;i<HOST_DEV_MAX_NUM;i++)
	{
		if(HostDevList[i].id == dev->id)
		{
			return i;
		}
	}
	
	return -1;
}


void devlist_get_content(rt_uint8_t pos, devListDef * dev)
{
	rt_memcpy(dev,&HostDevList[pos],sizeof(devListDef));
}

void devlist_pos_content(rt_uint8_t pos,devListDef *dev)
{
	rt_memcpy(&HostDevList[pos],dev,sizeof(devListDef));
}


rt_err_t dev_in_devlist(rt_uint32_t id)
{
	devListDef *dev;
	rt_int8_t i;
	rt_err_t result;
	
	dev = rt_calloc(1, sizeof(devListDef));

	dev->id = id;
	
	i = find_dev_devlist(dev);
	rt_kprintf("idididid=%d %d\n",i,id);
	if(i >= 0)
	{
		result = RT_EOK;
	}
	else
	{
		result = RT_ERROR;
	}
	rt_free(dev);
	
	return result;
}


rt_err_t get_devlist_info(devListDef *info)
{
	rt_int8_t i;
	
	i = find_dev_devlist(info);
	if(i < 0)
	{
		return RT_ERROR;
	}
	else
	{
		rt_memcpy(info,&HostDevList[i],sizeof(devListDef));
		return RT_EOK;
	}
}


rt_err_t add_to_devlist(devListDef *dev)
{
	rt_err_t result;
	rt_int8_t i;

	i = find_dev_devlist(dev);
	if(i > 0)//已经存在
	{
		return RT_EOK;
	}

	i = empty_pos_devlist();
	HostDevList[i].id = dev->id;
	rt_strncpy(HostDevList[i].name, dev->name, rt_strlen(dev->name));
	rt_strncpy(HostDevList[i].alarmTone,dev->alarmTone,rt_strlen(dev->alarmTone));
	rt_strncpy(HostDevList[i].alarmType,dev->alarmType,rt_strlen(dev->alarmType));
	rt_strncpy(HostDevList[i].uploadRd,dev->uploadRd,rt_strlen(dev->uploadRd));
	HostDevList[i].type = dev->type;
	
	return result;
}


rt_err_t remove_to_devlist(devListDef *dev)
{
	rt_int8_t i;

	i = find_dev_devlist(dev);
	if(i>0)
	{
		HostDevList[i].id = 0;
		return RT_EOK;
	}
	else
	{
		return RT_ERROR;
	}
}

void show_devlist(void)
{
	rt_int8_t i;

	for(i=0;i<HOST_DEV_MAX_NUM;i++)
	{
		if(HostDevList[i].id > 0)
		{
			rt_kprintf("\n#################################################\n");
			rt_kprintf("id=%d ",HostDevList[i].id);
			rt_kprintf("name=%s ",HostDevList[i].name);
			rt_kprintf("type=%d ",HostDevList[i].type);
			rt_kprintf("alarmType=%s ",HostDevList[i].alarmType);
			rt_kprintf("alarmTone=%s",HostDevList[i].alarmTone);
		}
	}
}

rt_err_t get_dev_type_string(rt_uint8_t type,rt_uint8_t *buf,rt_uint8_t size)
{
	rt_memset(buf,0,size);
	switch(type)
	{
		case 0:
		{
			rt_strncpy((char *)buf,"TYP,SWITCH|0",rt_strlen((const char *)"TYP,SWITCH|0"));
			break;
		}
		case 1:
		{
			rt_strncpy((char *)buf,"TYP,FIRE_SENSOR|1",rt_strlen((const char *)"TYP,FIRE_SENSOR|1"));
			break;
		}
		case 2:
		{
			rt_strncpy((char *)buf,"TYP,WARTER_SENSOR|2",rt_strlen((const char *)"TYP,WARTER_SENSOR|2"));
			break;
		}
		case 3:
		{
			rt_strncpy((char *)buf,"TYP,GAS_SENSOR|3",rt_strlen((const char *)"TYP,GAS_SENSOR|3"));
			break;
		}
		case 4:
		{
			rt_strncpy((char *)buf,"TYP,MOV_SENSOR|4",rt_strlen((const char *)"TYP,MOV_SENSOR|4"));
			break;
		}
		case 5:
		{
			rt_strncpy((char *)buf,"TYP,REMOTE_CTL|5",rt_strlen((const char *)"TYP,REMOTE_CTL|5"));
			break;
		}
		case 6:
		{
			rt_strncpy((char *)buf,"TYP,WLS_WARMING|6",rt_strlen((const char *)"TYP,WLS_WARMING|6"));
			break;
		}
		case 7:
		{
			rt_strncpy((char *)buf,"TYP,LIGHT|7",rt_strlen((const char *)"TYP,LIGHT|7"));
			break;
		}
		case 8:
		{
			rt_strncpy((char *)buf,"TYP,CURTAIN|8",rt_strlen((const char *)"TYP,CURTAIN|8"));
			break;
		}
		case 9:
		{
			rt_strncpy((char *)buf,"TYP,TEMP|9",rt_strlen((const char *)"TYP,TEMP|9"));
			break;
		}
		case 10:
		{
			rt_strncpy((char *)buf,"TYP,DOOR_SENSOR|10",rt_strlen((const char *)"TYP,DOOR_SENSOR|10"));
			break;
		}
		case 11:
		{
			rt_strncpy((char *)buf,"TYP,RAY_SENSOR|11" ,rt_strlen((const char *)"TYP,RAY_SENSOR|11" ));
			break;
		}
		case 12:
		{
			rt_strncpy((char *)buf,"TYP,LOCK|12",rt_strlen((const char *)"TYP,LOCK|12"));
			break;
		}
		case 13:
		{
			rt_strncpy((char *)buf,"TYP,CO_SENSOR|13",rt_strlen((const char *)"TYP,CO_SENSOR|13"));
			break;
		}
		case 14:
		{
			rt_strncpy((char *)buf,"TYP,INFRADE_TRANSMIT|14",rt_strlen((const char *)"TYP,INFRADE_TRANSMIT|14"));
			break;
		}
		case 15:
		{
			rt_strncpy((char *)buf,"TYP,WIND_LIGHT_RAIN|15",rt_strlen((const char *)"TYP,WIND_LIGHT_RAIN|15"));
			break;
		}
		case 16:
		{
			rt_strncpy((char *)buf,"TYP,BACKGOUND_MUSIC|16",rt_strlen((const char *)"TYP,BACKGOUND_MUSIC|16"));
			break;
		}
		case 17:
		{
			rt_strncpy((char *)buf,"TYP,PLUG|17",rt_strlen((const char *)"TYP,PLUG|17"));
			break;
		}
		case 18:
		{
			rt_strncpy((char *)buf,"TYP,E_WINDOW|18",rt_strlen((const char *)"TYP,E_WINDOW|18"));
			break;
		}
		case 19:
		{
			rt_strncpy((char *)buf,"TYP,COLOR_LED|19",rt_strlen((const char *)"TYP,COLOR_LED|19"));
			break;
		}
		case 20:
		{
			rt_strncpy((char *)buf,"TYP,LEVEL_LED|20",rt_strlen((const char *)"TYP,LEVEL_LED|20"));
			break;
		}
		case 21:
		{
			rt_strncpy((char *)buf,"TYP,COLOR_TEM_LED|21",rt_strlen((const char *)"TYP,COLOR_TEM_LED|21"));
			break;
		}
		case 22:
		{
			rt_strncpy((char *)buf,"TYP,IAS_DEV|22",rt_strlen((const char *)"TYP,IAS_DEV|22"));
			break;
		}
		case 23:
		{
			rt_strncpy((char *)buf,"TYP,IR_DEV|23",rt_strlen((const char *)"TYP,IR_DEV|23"));
			break;
		}
		case 24:
		{
			rt_strncpy((char *)buf,"TYP,IR_CONTROL|24",rt_strlen((const char *)"TYP,IR_CONTROL|24"));
			break;
		}
		case 25:
		{
			rt_strncpy((char *)buf,"TYP,EMERGENCY_BUTTOM|25",rt_strlen((const char *)"TYP,EMERGENCY_BUTTOM|25"));
			break;
		}
		case 26:
		{
			rt_strncpy((char *)buf,"TYP,SENCE_SWITCH|26",rt_strlen((const char *)"TYP,SENCE_SWITCH|26"));
			break;
		}
		case 27:
		{
			rt_strncpy((char *)buf,"TYP,LED_LOOP|27",rt_strlen((const char *)"TYP,LED_LOOP|27"));
			break;
		}
		case 28:
		{
			rt_strncpy((char *)buf,"TYP,LED_RAY|28",rt_strlen((const char *)"TYP,LED_RAY|28"));
			break;
		}
		case 29:
		{
			rt_strncpy((char *)buf,"TYP,FISH_FEED_DEV|29",rt_strlen((const char *)"TYP,FISH_FEED_DEV|29"));
			break;
		}
		case 30:
		{
			rt_strncpy((char *)buf,"TYP,SENSOR|30",rt_strlen((const char *)"TYP,SENSOR|30"));
			break;
		}
		case 31:
		{
			rt_strncpy((char *)buf,"TYP,FSK_RGB_LED|31",rt_strlen((const char *)"TYP,FSK_RGB_LED|31"));
			break;
		}
		case 32:
		{
			rt_strncpy((char *)buf,"TYP,IP_CAMERA|32",rt_strlen((const char *)"TYP,IP_CAMERA|32"));
			break;
		}
		case 33:
		{
			rt_strncpy((char *)buf,"TYP,DOOR_BELL|33",rt_strlen((const char *)"TYP,DOOR_BELL|33"));
			break;
		}
		case 34:
		{
			rt_strncpy((char *)buf,"TYP,KEY_BOARD|34",rt_strlen((const char *)"TYP,KEY_BOARD|34"));
			break;
		}
		case 35:
		{
			rt_strncpy((char *)buf,"TYP,VIBRATION|35",rt_strlen((const char *)"TYP,VIBRATION|35"));
			break;
		}
		case 36:
		{
			rt_strncpy((char *)buf,"TYP,GLASS_BREAK|36",rt_strlen((const char *)"TYP,GLASS_BREAK|36"));
			break;
		}
		default:
		{
			return RT_ERROR;
			//break;
		}
	}

	return RT_EOK;
}


rt_uint8_t get_listdev_vaild_num(void)
{
	rt_int8_t i;
	rt_uint8_t num = 0;
	
	for(i=0;i<HOST_DEV_MAX_NUM;i++)
	{
		if(HostDevList[i].id > 0)//有效ID
		{
			num++;
		}
	}

	return num;
}


rt_err_t make_dev_xml_string(rt_uint8_t *buf,rt_uint8_t pos)
{
	rt_int8_t i;
	rt_uint8_t num = 0;
	rt_uint8_t *tmpBuf;
	rt_uint8_t *tmpid;
	rt_err_t result;
	
	tmpBuf = rt_calloc(1,64);
	RT_ASSERT(tmpBuf);

	tmpid = rt_calloc(1, 32);
	RT_ASSERT(tmpid);

	for(i=0;i<HOST_DEV_MAX_NUM;i++)
	{
		if(HostDevList[i].id > 0)//有效ID
		{
			num++;
			if(num == pos)//位置正确
			{
				break;
			}
		}
	}

	if(i < HOST_DEV_MAX_NUM)
	{
		get_dev_type_string(HostDevList[i].type,tmpBuf,64);
		rt_memset(tmpid, 0, 32);
		rt_sprintf(tmpid, "%d", HostDevList[i].id);
		rt_sprintf(buf,"<L%d><DevNo>S32,0,256|%d</DevNo><DevType>%s</DevType>\
<Name>STR,%d|%s</Name><AreaNo>S32,0,32|1</AreaNo>\
<AlarmType>TYP,BURRGLAR|3</AlarmType>\
<AlarmTone>TYP,TONE|1</AlarmTone>\
<UploadRd>TYP,ON|1</UploadRd>\
<RfId>STR,%d|%d</RfId><WlId>STR,%d|%d</WlId>\
<CtlType>TYP,RF|0</CtlType><Online>TYP,NO|1</Online>\
<Sn>S32,0,4|1</Sn><S0><PirStatus>S32,0,2147483647|0</PirStatus></S0></L%d>",
				0,pos,tmpBuf,rt_strlen(HostDevList[i].name),
				HostDevList[i].name,rt_strlen(tmpid),HostDevList[i].id,rt_strlen(tmpid),
				HostDevList[i].id,0);
		result = RT_EOK;
	}
	else
	{
		result = RT_ERROR;
	}

	rt_free(tmpBuf);
	rt_free(tmpid);

	return result;
}


rt_uint8_t make_devlist_xml_string(rt_uint8_t *buf)
{
	rt_int8_t i;
	rt_uint8_t *node;
	rt_uint8_t pos = 0;
	rt_uint8_t devno = 1;
	rt_uint8_t *tmpBuf;
	rt_uint8_t *tmpid;
	rt_uint8_t *head;

	head = buf;
	tmpBuf = rt_calloc(1,64);
	RT_ASSERT(tmpBuf);

	tmpid = rt_calloc(1, 32);
	RT_ASSERT(tmpid);
	
	node = rt_calloc(1,1024);
	RT_ASSERT(node);
	for(i=0;i<HOST_DEV_MAX_NUM;i++)
	{
		if(HostDevList[i].id > 0)
		{
			get_dev_type_string(HostDevList[i].type,tmpBuf,64);
			rt_memset(tmpid, 0, 32);
			rt_sprintf(tmpid, "%d", HostDevList[i].id);
			rt_sprintf(node,"<L%d><DevNo>S32,0,256|%d</DevNo><DevType>%s</DevType>\
<Name>STR,%d|%s</Name><AreaNo>S32,0,32|1</AreaNo>\
<AlarmType>TYP,BURRGLAR|3</AlarmType>\
<AlarmTone>TYP,TONE|1</AlarmTone>\
<UploadRd>TYP,ON|1</UploadRd>\
<RfId>STR,%d|%d</RfId><WlId>STR,%d|%d</WlId>\
<CtlType>TYP,RF|0</CtlType><Online>TYP,NO|1</Online>\
<Sn>S32,0,4|1</Sn><S0><PirStatus>S32,0,2147483647|0</PirStatus></S0></L%d>",
				pos,devno,tmpBuf,rt_strlen(HostDevList[i].name),HostDevList[i].name,rt_strlen(tmpid),HostDevList[i].id,rt_strlen(tmpid),HostDevList[i].id,pos);
			rt_strncpy(head,node,rt_strlen(node));
			pos++;
			devno++;
			head += rt_strlen(node);
		}
		
	}

	rt_free(tmpid);
	rt_free(tmpBuf);
	rt_free(node);

	return pos;
}



rt_err_t devlist_save(void)
{
	rt_device_t dev;
	rt_size_t size;
	rt_uint32_t len;

	len = (sizeof(devListDef)*HOST_DEV_MAX_NUM);
	rt_kprintf("len = %d\n",len);

	dev = eeprom_device_open();
	size = rt_device_write(dev, EEPROM_DEVLIST_ADDR, HostDevList, len);
	eeprom_device_close(dev);
	if(size != len)
	{
		return RT_ERROR;	
	}
	return RT_EOK;
}


rt_err_t devlist_loader(void)
{
	rt_device_t dev;
	rt_size_t size;
	
	dev = eeprom_device_open();
	size = rt_device_read(dev, EEPROM_DEVLIST_ADDR, HostDevList, (sizeof(devListDef)*HOST_DEV_MAX_NUM));
	eeprom_device_close(dev);
	if(size != (sizeof(devListDef)*HOST_DEV_MAX_NUM))
	{
		return RT_ERROR;	
	}
	return RT_EOK;
}

void devlist_init(void)
{
	rt_uint8_t i;
	rt_err_t result;
	
	devlist_loader();
	for(i=0;i<HOST_DEV_MAX_NUM-1;i++)
	{
		if(HostDevList[i].id != HostDevList[i+1].id)
		{
			return;
		}
	}
	for(i=0;i<HOST_DEV_MAX_NUM;i++)
	{
		HostDevList[i].id = 0;
		HostDevList[i].type = 0;
		rt_memset(HostDevList[i].name ,0 ,12);
		rt_memset(HostDevList[i].alarmType ,0 ,32);
		rt_memset(HostDevList[i].alarmTone ,0 ,32);
		rt_memset(HostDevList[i].uploadRd ,0 ,32);
	}
	result = devlist_save();
	if(result == RT_EOK)
	{
		rt_kprintf("保存设备列表成功\n");
	}
	else
	{
		rt_kprintf("设备列表保存异常\n");
	}
}

#ifdef RT_USING_FINSH 
#include <finsh.h>

void test_list(void)
{
	rt_uint8_t *buf;

	buf = rt_calloc(1,2048);
	make_devlist_xml_string(buf);

	//rt_kprintf("%s",buf);
	show_buf_string(buf, rt_strlen(buf));
	rt_free(buf);
}
FINSH_FUNCTION_EXPORT(test_list,"xml test");
#endif


